package com.yisu.expressway.onedollar.adapter;

import java.util.List;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.will.network.images.round.CircleImageView;
import com.yisu.expressway.R;
import com.yisu.expressway.onedollar.model.JoinRecordItem;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Kaven on 2016/12/15.
 */

public class JoinRecordAdapter extends BaseAdapter {
    private Context mContext;
    private List<JoinRecordItem> mData;

    public JoinRecordAdapter(Context context, List<JoinRecordItem> data) {
        mContext = context;
        mData = data;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public JoinRecordItem getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.onedollar_join_record_item, null);
            holder.mAvatarIv = (CircleImageView) convertView.findViewById(R.id.iv_cover);
            holder.mNameTv = (TextView) convertView.findViewById(R.id.tv_winner);
            holder.mTimeTv = (TextView) convertView.findViewById(R.id.tv_time);
            holder.mJoinTimeTv = (TextView) convertView.findViewById(R.id.tv_joinnum);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JoinRecordItem item = getItem(position);
        if (item != null) {
            holder.mAvatarIv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            final String userHeadImg = item.userHeadImg;
            if (!TextUtils.isEmpty(userHeadImg)) {
                Glide.with(mContext).load(item.userHeadImg).asBitmap()
                        .placeholder(R.drawable.avatar_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
                        .animate(R.anim.fade_in_image).into(holder.mAvatarIv);
            } else {
                Glide.clear(holder.mAvatarIv);
                holder.mAvatarIv.setImageDrawable(
                        mContext.getResources().getDrawable(R.drawable.avatar_default));
            }
            String userName = item.userName;
            StringBuilder stringBuilder = new StringBuilder();
            if (!TextUtils.isEmpty(userName)) {

                stringBuilder.append(userName);

            }
            holder.mNameTv.setText(stringBuilder.toString());
            String createTime = item.createTime;
            if (!TextUtils.isEmpty(createTime)) {
                holder.mTimeTv.setText(createTime);
            }
            String format = String.format(
                    mContext.getResources().getString(R.string.one_dollar_times), item.joinNum);
            holder.mJoinTimeTv.setText(format);
        }

        return convertView;
    }

    public class ViewHolder {
        public CircleImageView mAvatarIv;
        public TextView mNameTv;
        public TextView mTimeTv;
        public TextView mJoinTimeTv;
    }
}
